package com.sky.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {


    @Resource
    CategoryMapper categoryMapper;

    @Resource
    DishMapper dishMapper;

    @Resource
    SetmealMapper setmealMapper;



    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {

        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        String name = categoryPageQueryDTO.getName();
        Integer type = categoryPageQueryDTO.getType();


        PageHelper.startPage(page, pageSize);
        Page<Category> categories = categoryMapper.pageQuery(name, type);
        List<Category> records = categories.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(categories.getTotal());
        pageResult.setRecords(records);

        return pageResult;
    }

//    @Override
//    public PageResult listPage(Integer type) {
//        Page<Category> listByType = categoryMapper.getListByType(type);
//
//        List<Category> listByTypeResult = listByType.getResult();
//
//        PageResult pageResult = new PageResult();
//        pageResult.setRecords(listByTypeResult);
//        pageResult.setTotal(listByType.getTotal());
//
//        return pageResult;
//    }

    public List<Category> list(Integer type) {
        List<Category> listByType = categoryMapper.getListByType(type);

        return listByType;
    }


    @Override
    public void changeStatus(Integer status, Long id) {
        Long currentId = BaseContext.getCurrentId();
        if(currentId != 1L) {
            throw new BaseException(MessageConstant.NO_AUTH);
        }
        if(status == null || id == null) {
            throw new BaseException(MessageConstant.PARAM_ERROR);
        }
        categoryMapper.update(Category.builder().id(id).status(status).build());
    }

    @Override
    public void addNewCategory(CategoryDTO categoryDTO) {
        Long currentId = BaseContext.getCurrentId();
        if(currentId != 1L) {
            throw new BaseException(MessageConstant.NO_AUTH);
        }
        Long id = categoryDTO.getId();
        Integer type = categoryDTO.getType();
        String name = categoryDTO.getName();
        Integer sort = categoryDTO.getSort();
        if(type == null || sort == null) {
            throw new BaseException(MessageConstant.PARAM_ERROR);
        }

        if(!StrUtil.isNotBlank(name)) {
            throw new BaseException(MessageConstant.PARAM_ERROR);
        }

        Category categoryByName = categoryMapper.getByName(name);
        if(categoryByName != null) {
            throw new BaseException("分类名" + MessageConstant.ALREADY_EXISTS);
        }

        Category category = Category.builder()
                .id(id)
                .type(type)
                .name(name)
                .sort(sort)
//                .createTime(LocalDateTime.now())
//                .updateTime(LocalDateTime.now())
//                .updateUser(currentId)
//                .createUser(currentId)
                .status(StatusConstant.DISABLE)
                .build();

        categoryMapper.save(category);

    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Long currentId = BaseContext.getCurrentId();
        if(currentId != 1L) {
            throw new BaseException(MessageConstant.NO_AUTH);
        }
        if(categoryDTO == null) {
            throw new BaseException(MessageConstant.PARAM_ERROR);
        }
        Long id = categoryDTO.getId();
//        Integer type = categoryDTO.getType();
        String name = categoryDTO.getName();
        Integer sort = categoryDTO.getSort();
        if(id == null || sort == null || !StrUtil.isNotBlank(name)) {
            throw new BaseException(MessageConstant.PARAM_ERROR);
        }

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(currentId);

        categoryMapper.update(category);

    }

    @Override
    public void deleteById(Long id) {
        Integer count = dishMapper.countByCategoryId(id);
        if(count > 0) {
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        count = setmealMapper.countByCategoryId(id);
        if(count > 0) {
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.deleteById(id);
    }
}
